

package com.bei_en.teacher.admin.service.user;

import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bei_en.teacher.admin.service.BaseService;
import com.bei_en.teacher.admin.model.user.bean.UserFinance;
import com.bei_en.teacher.admin.model.user.vo.UserFinanceVo;
import com.bei_en.teacher.admin.model.user.condition.UserFinanceCondition;
import com.bei_en.teacher.admin.persistence.dao.user.UserFinanceDao;

/**
 * 
 * @author Guocg
 * @date 2016-10-2 19:23:07
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class UserFinanceService extends BaseService{

    @Autowired
    private UserFinanceDao userFinanceDao;

    public void insert(UserFinanceVo model) {

    	 userFinanceDao.insert(model.getUserFinance());
    }

    /**
     * 插入并返回生成的id
     * @param model
     * @return
     */
    public Integer merge(UserFinanceVo model) {

	userFinanceDao.insert(model.getUserFinance());
	return userFinanceDao.findLastInsertedId();

    }

    public void delete(Integer modelInteger) {

    	userFinanceDao.delete(modelInteger);
    }

    public UserFinanceVo findById(Integer modelInteger) {

    	UserFinanceVo model = new UserFinanceVo(userFinanceDao.findById(modelInteger));
    	return model;
    }

    public void update(UserFinanceVo model) {

    	userFinanceDao.update(model.getUserFinance());
    }

    /**
     * 动态更新,只更新非null的值
     * @param model
     */
    public void updateSelective(UserFinanceVo model) {

    	userFinanceDao.updateSelective(model.getUserFinance());
    }

    public int countAll() {

    	return userFinanceDao.countAll();
    }

    public List<UserFinanceVo> findAll() {

		List<UserFinanceVo> list = new ArrayList<UserFinanceVo>();
		List<UserFinance> list1 = userFinanceDao.findAll();
		for (UserFinance model : list1) {
		    list.add(new UserFinanceVo(model));
		}
		return list;
    }

    public List<Integer> findAllIds() {

    	return userFinanceDao.findAllIds();
    }

    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<UserFinanceVo> findUserFinancePage(UserFinanceCondition vo) {

		Integer totalCount = searchUserFinanceNum(vo);
		List<UserFinanceVo> list = new ArrayList<UserFinanceVo>();
		vo.setTotalCount(totalCount);
	
		if (totalCount>0) {
		    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "id");
		    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");
	
		    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		    List<UserFinance> list1 = userFinanceDao.findPageBreakByCondition(vo, rb);
		    for (UserFinance model : list1) {
		    	list.add(new UserFinanceVo(model));
		    }
		}

		return list;
    }

    private Integer searchUserFinanceNum(UserFinanceCondition Vo) {

    	return userFinanceDao.findNumberByCondition(Vo);
    }

}
